package io.wanghaoran.project.module.system.controller;

import io.wanghaoran.project.framework.security.util.SecurityUtils;
import io.wanghaoran.project.module.system.entity.UserTokensLog;
import io.wanghaoran.project.module.system.service.UserTokensLogService;
import io.wanghaoran.project.common.base.domain.Result;
import io.wanghaoran.project.common.base.domain.TableDataInfo;
import io.wanghaoran.project.common.annotation.CrudLog;
import io.wanghaoran.project.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotNull;

import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;

import org.springframework.stereotype.Controller;
import io.wanghaoran.project.common.base.controller.BaseController;

import javax.validation.Valid;
import java.util.Objects;
import java.util.List;
/**
* <p>
*  前端控制器
* </p>
* @author authorname
* @since 2025-07-30
*/
@Api(tags = "")
@Controller
@RequestMapping("/system/userTokensLog")
public class UserTokensLogController extends BaseController {

  private static final String SUFFIX = "system/userTokensLog";

  @Autowired
  private UserTokensLogService userTokensLogService;

  @GetMapping("/listPage")
  public String listPage(){
    return SUFFIX + "/list";
  }

  @ApiOperation(value = "分页列表", response = UserTokensLog.class)
  @ApiImplicitParams({
    @ApiImplicitParam(name = "page", value = "第几页", dataType = "Integer", paramType="query"),
    @ApiImplicitParam(name = "limit", value = "每页记录数", dataType = "Integer", paramType="query")
  })
  @PreAuthorize("hasAuthority('userTokensLog:list')")
  @GetMapping(value = "/page")
  @ResponseBody
  public TableDataInfo page(UserTokensLog param) {
    startPage();

      if (!Objects.equals(SecurityUtils.getUserName(), "admin")) {
          param.setUserName(SecurityUtils.getUserName());
      }
    List<UserTokensLog> data = userTokensLogService.page(param);
    return getDataTable(data);
  }


   @GetMapping("/editPage/{id}")
   public String editPage(@Valid @NotNull(message = "id不能为空") @PathVariable Long id, ModelMap map){
    Object data = userTokensLogService.info(id);
    map.put("userTokensLog",data);
    return SUFFIX + "/edit";
  }

  @GetMapping("/addPage")
  public String addPage(){
    return SUFFIX + "/add";
  }

  @CrudLog(title = "新增",businessType = BusinessType.INSERT)
  @ApiOperation(value = "新增")
  @PreAuthorize("hasAuthority('userTokensLog:add')")
  @PostMapping(value = "/add")
  @ResponseBody
  public Result add(@RequestBody @Validated UserTokensLog param,BindingResult bindingResult) {
    if (bindingResult.hasErrors()){
        return Result.failure(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
    }
    userTokensLogService.add(param);
    return Result.success();
  }

  @CrudLog(title = "修改",businessType = BusinessType.UPDATE)
  @ApiOperation(value = "修改")
  @PreAuthorize("hasAuthority('userTokensLog:edit')")
  @PostMapping(value = "/modify")
  @ResponseBody
  public Result modify(@RequestBody @Validated UserTokensLog param,BindingResult bindingResult) {
    if (bindingResult.hasErrors()){
        return Result.failure(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
    }
    userTokensLogService.modify(param);
    return Result.success();
  }

  @CrudLog(title = "删除(单个条目)",businessType = BusinessType.DELETE)
  @ApiOperation(value = "删除(单个条目)")
  @PreAuthorize("hasAuthority('userTokensLog:del')")
  @DeleteMapping(value = "/remove/{id}")
  @ResponseBody
  public Result remove(@Valid @NotNull(message = "id不能为空") @PathVariable Long id) {
    userTokensLogService.remove(id);
    return Result.success();
  }

  @CrudLog(title = "删除(多个条目)",businessType = BusinessType.DELETE)
  @ApiOperation(value = "删除(多个条目)")
  @PreAuthorize("hasAuthority('userTokensLog:del')")
  @PostMapping(value = "/removes")
  @ResponseBody
  public Result removes(@RequestBody @Valid List<Long> ids) {
     userTokensLogService.removes(ids);
     return Result.success();
   }
}
